package com.example.demo.controller;

import com.example.demo.service.OraService;
import com.example.demo.service.PersonnelService;
import com.example.demo.util.R;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Log4j2
@RestController
public class PersonnelController {

    private static final Logger LOG = LoggerFactory.getLogger(PersonnelController.class);

    @Resource
    private PersonnelService personnelservice;

    @Resource
    private OraService oraService;



    //连接msyql
    @RequestMapping(value = "/selectTest", method = RequestMethod.POST)
    public Object selectTest() {
        Map params = new HashMap();
        params.put("sysId","ztgl-app-074f9e47-5941-430e-b83a-08ba26d4ee20");
        Map<String, Object> data = personnelservice.selectTest(params);
        System.out.println("结果："+data);

        return R.ok(data);
    }

    //连接oracle
    @RequestMapping(value = "/selectTestOracle", method = RequestMethod.POST)
//    @RequiresPermissions("obj:assetsmaster:selectSysUserRoleOnUserId")
    public Object selectTestOracle() {
        Map params = new HashMap();
        params.put("code","TB_BIZ_A");
        Map<String, Object> data = personnelservice.selectTestOracle(params);
        System.out.println("结果："+data);
//        LOG.toString();
//        LOG.error("错误", "");
        return R.ok(data);
    }


    //测试多数据源
    @RequestMapping(value = "/Test", method = RequestMethod.GET)
    public Object Test() {
        Map<String, Object> data = personnelservice.Test();
        System.out.println("结果："+data);
        return R.ok(data);

    }



    //定时任务
    @RequestMapping(value = "/selectCron", method = RequestMethod.POST)
    public Object selectCron() {
        try {
        Map params = new HashMap();
        params.put("cronId","1");

        Map<String, Object> data = personnelservice.selectCron(params);
        System.out.println("结果："+data);

        return R.ok(data);
    } catch (Exception e) {
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //定时任务修改时间配置，修改后只能24小时后生效 全量
    @RequestMapping(value = "/updateCronAll", method = RequestMethod.POST)
    public Object updateCronAll(@RequestBody Map<String, Object> params) {
        /*"fenVal" : fenVal, 0 23 15 * * ?  0 08 06 * * ?  0 11 1 0 0 3
            "millsVal" : millsVal*/
        try {
            Integer cronId = Integer.parseInt(String.valueOf(params.get("cronId")));
        String fenVal = String.valueOf(params.get("fenVal"));
        String millsVal = String.valueOf(params.get("millsVal"));
        String zhouVal = String.valueOf(params.get("zhouVal"));
        String cron = "0 "+millsVal+" "+fenVal+" * * "+zhouVal;

        params.put("cron",cron);
        personnelservice.updateCron(params);
        return R.ok();
    } catch (Exception e) {
            LOG.error("定时任务修改错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //定时任务修改时间配置，修改后只能24小时后生效 增量
    @RequestMapping(value = "/updateCronAdd", method = RequestMethod.POST)
    public Object updateCronAdd(@RequestBody Map<String, Object> params) {
        try {
            String fenVal = String.valueOf(params.get("fenVal"));
            String millsVal = String.valueOf(params.get("millsVal"));
            String cron = "0 "+millsVal+" "+fenVal+" * * ?";

            params.put("cron",cron);
            personnelservice.updateCron(params);
            return R.ok();
        } catch (Exception e) {
            LOG.error("定时任务修改错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }


    //表1
    @RequestMapping(value = "/OraAlarmitems", method = RequestMethod.POST)
    public Object OraAlarmitems() {
        try {

        Map<String, Object> data = oraService.OraAlarmitems();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.info("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表2
    @RequestMapping(value = "/OraBasicinfo", method = RequestMethod.POST)
    public Object OraBasicinfo() {
        try {

        Map<String, Object> data = oraService.OraBasicinfo();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表3
    @RequestMapping(value = "/OraCheckcar", method = RequestMethod.POST)
    public Object OraCheckcar() {
        try {

        Map<String, Object> data = oraService.OraCheckcar();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表4
    @RequestMapping(value = "/OraChecksize", method = RequestMethod.POST)
    public Object OraChecksize() {
        try {


        Map<String, Object> data = oraService.OraChecksize();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表5
    @RequestMapping(value = "/OraCheckwheelscar", method = RequestMethod.POST)
    public Object OraCheckwheelscar() {
        try {

        Map<String, Object> data = oraService.OraCheckwheelscar();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表6
    @RequestMapping(value = "/OraAlOraOverdemarcationrecordarmitems", method = RequestMethod.POST)
    public Object OraOverdemarcationrecord() {
        try {

        Map<String, Object> data = oraService.OraOverdemarcationrecord();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表7
    @RequestMapping(value = "/OraJczb", method = RequestMethod.POST)
    public Object OraJczb() {
        try {

        Map<String, Object> data = oraService.OraJczb();
        System.out.println("结果："+data);
        return R.ok(data);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }



    //以下是各个表全量导入
    //表1
    @RequestMapping(value = "/OraAlarmitemsPage", method = RequestMethod.POST)
    public Object OraAlarmitemsPage() {

        try {
        oraService.OraAlarmitemsPage();
            System.out.println("导入成功-----");
        return R.ok();
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表2
    @RequestMapping(value = "/OraBasicinfoPage", method = RequestMethod.POST)
    public Object OraBasicinfoPage() {

       try {
        oraService.OraBasicinfoPage();
           System.out.println("导入成功-----");
        return R.ok();
    } catch (Exception e) {
           System.out.println(e.getMessage());
           LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表3
    @RequestMapping(value = "/OraCheckcarPage", method = RequestMethod.POST)
    public Object OraCheckcarPage() {

        try {
        oraService.OraCheckcarPage();
            System.out.println("导入成功-----");
        return R.ok();
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表4
    @RequestMapping(value = "/OraChecksizePage", method = RequestMethod.POST)
    public Object OraChecksizePage() {

        try {
        oraService.OraChecksizePage();
            System.out.println("导入成功-----");
        return R.ok();
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表5
    @RequestMapping(value = "/OraCheckwheelscarPage", method = RequestMethod.POST)
    public Object OraCheckwheelscarPage() {

        try {
        oraService.OraCheckwheelscarPage();
            System.out.println("导入成功-----");
        return R.ok();
    } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
        return R.error(e.getMessage());
    }
    }

    //表6
    @RequestMapping(value = "/OraOverdemarcationrecordPage", method = RequestMethod.POST)
    public Object OraOverdemarcationrecordPage() {

        try {
            oraService.OraOverdemarcationrecordPage();
            System.out.println("导入成功-----");
            return R.ok();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表7
    @RequestMapping(value = "/OraJczbPage", method = RequestMethod.POST)
    public Object OraJczbPage() {

        try {
            oraService.OraJczbPage();
            System.out.println("导入成功-----");
            return R.ok();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }


    //一次性导入
    @ RequestMapping(value = "/AllImportInfo", method = RequestMethod.POST)
    public Object AllImport() {

        try{
            //表1
            oraService.OraAlarmitemsPage();System.out.println("1导入成功-----");
            //表2
            oraService.OraBasicinfoPage();System.out.println("2导入成功-----");
            //表3
            oraService.OraCheckcarPage();System.out.println("3导入成功-----");
            //表4
            oraService.OraChecksizePage();System.out.println("4导入成功-----");
            //表5
            oraService.OraCheckwheelscarPage();System.out.println("5导入成功-----");
            //表6
            oraService.OraOverdemarcationrecordPage();System.out.println("6导入成功-----");
            //表7
            oraService.OraJczbPage();System.out.println("7导入成功-----");
            return R.ok();
        }catch (Exception e){
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }

    }

    public static void main(String[] args) {
//        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = dateFormat.format(new Date());
        System.out.println(createTime);
    }



    //以下是根据最大时间最小时间来查增量
    //表1
    @RequestMapping(value = "/OraAlarmitemsPageMax", method = RequestMethod.POST)
    public Object OraAlarmitemsPageMax() {

        try {
            Map<String, Object> data = oraService.OraAlarmitemsPageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表2
    @RequestMapping(value = "/OraBasicinfoPageMax", method = RequestMethod.POST)
    public Object OraBasicinfoPageMax() {

        try {
            Map<String, Object> data = oraService.OraBasicinfoPageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表3
    @RequestMapping(value = "/OraCheckcarPageMax", method = RequestMethod.POST)
    public Object OraCheckcarPageMax() {

        try {
            Map<String, Object> data = oraService.OraCheckcarPageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表4
    @RequestMapping(value = "/OraChecksizePageMax", method = RequestMethod.POST)
    public Object OraChecksizePageMax() {

        try {
            Map<String, Object> data = oraService.OraChecksizePageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表5
    @RequestMapping(value = "/OraCheckwheelscarPageMax", method = RequestMethod.POST)
    public Object OraCheckwheelscarPageMax() {

        try {
            Map<String, Object> data = oraService.OraCheckwheelscarPageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }


    //表6
    @RequestMapping(value = "/OraOverdemarcationrecordPageMax", method = RequestMethod.POST)
    public Object OraOverdemarcationrecordPageMax() {

        try {
            Map<String, Object> data = oraService.OraOverdemarcationrecordPageMax();
            System.out.println("增量导入成功------");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }

    //表7
    @RequestMapping(value = "/OraJczbPageMax", method = RequestMethod.POST)
    public Object OraJczbPageMax() {

        try {
            Map<String, Object> data = oraService.OraJczbPageMax();
            System.out.println("增量导入成功-----");
            return R.ok(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }
    }


    //一次性导入
    @ RequestMapping(value = "/ImportOnTime", method = RequestMethod.POST)
    public Object ImportOnTime() {

        try{
            //表1
            oraService.OraAlarmitemsPageMax();System.out.println("1导入成功-----");
            //表2
            oraService.OraBasicinfoPageMax();System.out.println("2导入成功-----");
            //表3
            oraService.OraCheckcarPageMax();System.out.println("3导入成功-----");
            //表4
            oraService.OraChecksizePageMax();System.out.println("4导入成功-----");
            //表5
            oraService.OraCheckwheelscarPageMax();System.out.println("5导入成功-----");
            //表6
            oraService.OraOverdemarcationrecordPageMax();System.out.println("6导入成功-----");
            //表7
            oraService.OraJczbPageMax();System.out.println("7导入成功-----");
            return R.ok();
        }catch (Exception e){
            System.out.println(e.getMessage());
            LOG.error("错误", e.getMessage());
            return R.error(e.getMessage());
        }

    }





}







